home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / actlib13.zip / STRINGS.ZIP / STRTOHEX.C < prev    next >
C/C++ Source or Header  |  1993-02-25  |  969b  |  38 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4. #include <stdio.h>
  5.  
  6. /***
  7.  *  Function    :   strtohex
  8.  *
  9.  *  Description :   convert a string containing an hexadecimal
  10.  *                  representation into an hexadecimal flow of bytes.
  11.  *
  12.  *  Parameters  :   char *string   in/out
  13.  *
  14.  *  Return      :   length of hexadecimal flow of bytes.
  15.  *                  -1 on error
  16.  *
  17.  *  OS/Compiler :   All
  18.  ***/
  19.  
  20. int strtohex( char *string )
  21.  
  22. { int len = 0;
  23.   char *ptr = string, buffer[2];
  24.  
  25.   /* if string length is even, first char is a full number */
  26.   if ( strlen(string) % 2 ) { if ( sscanf(ptr, "%01x", buffer) != 1 ) return -1;
  27.                               *ptr++ = *buffer;
  28.                               len = 1;
  29.                             }
  30.  
  31.   while ( *ptr )
  32.         { if ( sscanf( ptr, "%02x", string + len++) != 1 ) return -1;
  33.           ptr += 2;
  34.         }
  35.  
  36.   return len;
  37. }
  38.